/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.fpsampayo.fin2006;

import es.fpsampayo.recursos.Conexion;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author fran
 */
public abstract class Listado_IMP {


    public static DefaultTableModel cargarDatosPadron (String concello) {

        DefaultTableModel modelo = new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
            
        };
        try {
            String sql = "SELECT * FROM public.bien_inmueble WHERE concello = \'" + concello + "\';";
            System.out.println(sql);
            Conexion conexion = new Conexion();
            ResultSet rs = conexion.Select(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int ncol = metadata.getColumnCount();
            String campos[] = new String [ncol];
            for ( int i = 0; i < ncol ; i++)
                campos[i] = metadata.getColumnLabel(i+1);
            modelo.setColumnIdentifiers(campos);
            while (rs.next()){
                Object[] fila = new Object[ncol];
                for ( int i = 0; i < ncol; i++){
                    try{
                        fila[i] = rs.getObject(i + 1);
                    } catch (SQLException ex) {
                        Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                modelo.addRow(fila);
            }

            conexion.Desconectar();

        } catch (SQLException ex) {
            Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        return modelo;

    }

    public static DefaultTableModel cargarDatos (){

        DefaultTableModel modelo = new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

        };
        try {
            String sql = "SELECT * FROM public.parcelas";
            Conexion conexion = new Conexion();
            ResultSet rs = conexion.Select(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int ncol = metadata.getColumnCount();
            String campos[] = new String [ncol];
            for ( int i = 0; i < ncol ; i++)
                campos[i] = metadata.getColumnLabel(i+1);
            modelo.setColumnIdentifiers(campos);
            while (rs.next()){
                Object[] fila = new Object[ncol];
                for ( int i = 0; i < ncol; i++){
                    try{
                        fila[i] = rs.getObject(i + 1);
                    } catch (SQLException ex) {
                        Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                modelo.addRow(fila);
            }
            conexion.Desconectar();

        } catch (SQLException ex) {
            Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        return modelo;
    }
    
    public static DefaultTableModel cargarDatosCat (String concello){

        DefaultTableModel modelo = new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

        };
        try {
            String sql = "SELECT * FROM cat.finca WHERE concello = \'" + concello + "\' ORDER BY referencia_catastral;";
            Conexion conexion = new Conexion();
            ResultSet rs = conexion.Select(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int ncol = metadata.getColumnCount();
            String campos[] = new String [ncol];
            for ( int i = 0; i < ncol ; i++)
                campos[i] = metadata.getColumnLabel(i+1);
            modelo.setColumnIdentifiers(campos);
            while (rs.next()){
                Object[] fila = new Object[ncol];
                for ( int i = 0; i < ncol; i++){
                    try{
                        fila[i] = rs.getObject(i + 1);
                    } catch (SQLException ex) {
                        Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                modelo.addRow(fila);
            }
            conexion.Desconectar();

        } catch (SQLException ex) {
            Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        return modelo;
    }
}
